package com.itelecom.oss.mobile.dao;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.net.Uri;


import com.itelecom.oss.Constants;
import com.itelecom.oss.mobile.domains.DeviceForMobile;
import com.itelecom.oss.mobile.domains.Users;

public class WebServiceDAOImpl implements WebServiceDAO {

    private ParserFlow pf;
    DefaultHttpClient httpClient;

    public WebServiceDAOImpl() {
        pf = new ParserFlow();
        httpClient = new DefaultHttpClient();
    }

//	private ParserFlow getPf() {
//		return pf;
//	}
//
//	public void setPf(ParserFlow pf) {
//		this.pf = pf;
//	}


    public List<DeviceForMobile> findDevicesByState(String state) {
        Uri.Builder uri = ParserFlow.buildGetMethod(Constants.API_DEVICE_FIND_BY_STATE);
//        uri.appendQueryParameter(Constants.PARAM_DEVICESTATE, state);
        HttpGet get = new HttpGet(uri.build().toString());

        final List<DeviceForMobile> devices = new ArrayList<DeviceForMobile>();
        
        DeviceForMobile dfm = new DeviceForMobile("10","Harkivske 17","ForInstallation","Switch 24 LRE","Test Switch","Some LRE24 device");
        DeviceForMobile dfm1 = new DeviceForMobile("20","Skovorody 2","ForInstallation","Switch 48 LRE","Test Switch","Some LRE48 device");
        DeviceForMobile dfm2 = new DeviceForMobile("30","Borshchahivska 100500","ForInstallation","Switch 24","Test Switch","Some device");
        devices.add(dfm);
        devices.add(dfm1);
        devices.add(dfm2);
        
        try {


            pf.executeRequest(get, new ResponseHandler() {
                public void handleResponse(InputStream in) throws IOException {
                    pf.parseResponse(in, new ResponseParser() {
                        public void parseResponse(XmlPullParser parser)
                                throws XmlPullParserException, IOException {
                            ParseDomain.parseDevices(parser, devices);
                        }
                    });
                }
            });

            return devices;
        } catch (IOException e) {
            android.util.Log.e("######", "Could not find devices with state: " + state);
        }
        return  devices;

    }


    public Boolean findUserWithPassword(Users users) {
    	
        Uri.Builder uri = ParserFlow.buildGetMethod(Constants.API_USER_FIND_BY_LOGIN_PASSWORD);
//        uri.appendQueryParameter(Constants.PARAM_LOGIN, users.getLogin());
//        uri.appendQueryParameter(Constants.PARAM_PASSWORD, users.getPassword());

        HttpGet get = new HttpGet(uri.build().toString());
        final Boolean result = true;
        
        try {
        	
            pf.executeRequest(get, new ResponseHandler() {
                public void handleResponse(InputStream in) throws IOException {
                    pf.parseResponse(in, new ResponseParser() {
                        public void parseResponse(XmlPullParser parser)
                                throws XmlPullParserException, IOException {
                        			ParseDomain.parseUser(parser, result);
                        }
                    });
                }
            });
            return result;
        } catch (IOException e) {
            android.util.Log.e("######", "Could not find the user with login: " + users.getLogin());
        }

        return result;
    }

    public void setDeviceState(DeviceForMobile device) {
        Uri.Builder uri = ParserFlow.buildGetMethod(Constants.API_DEVICE_SET_STATE_BY_ID);
//        uri.appendQueryParameter(Constants.PARAM_DEVICEID, device.getId());

        HttpPost post = new HttpPost(uri.build().toString());
        post.addHeader("Accept", "text/xml");
        post.addHeader("Content-Type", "application/xml");

        try {


            String xmlContentToSend = ParseDomain.DeviceToXml(device);
            HttpHost targetHost = new HttpHost(Constants.API_REST_HOST, Constants.API_REST_PORT, Constants.API_REST_PROTOCOL);

            StringEntity entity = new StringEntity(xmlContentToSend, "UTF-8");
            entity.setContentType("application/xml");
            post.setEntity(entity);


            // execute is a blocking call, it's best to call this code in a thread separate from the ui's
            HttpResponse response = httpClient.execute(targetHost, post);

            // Have your way with the response
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}